package com.link_with_health.controller;

import com.alibaba.fastjson.JSONObject;
import com.link_with_health.common.Page;
import com.link_with_health.common.Result;
import com.link_with_health.common.TokenMap;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.model.DoctorEntity;
import com.link_with_health.model.HospitalEntity;
import com.link_with_health.service.HospitalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/api/hospital")
public class HospitalController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String AUTH_TOKEN = "Authorization";
    private TokenMap tokenMap = TokenMap.getInstance();

    @Resource(name = "hospitalService")
    HospitalService hospitalService;

    /**
     * 获取医院信息
     * @param json 数据
     * @param request 请求
     * @return
     */
    @RequestMapping("/findHospitalInfo")
    @ResponseBody
    public Result findHospitalInfo(@RequestBody JSONObject json
            , HttpServletRequest request){
        String msg = "请求失败 无授权信息";
        String token = request.getHeader(AUTH_TOKEN);

        String method = (String) json.get("method");
        try {
            HashMap<String,Object> params =
                    (HashMap<String,Object>)json.get("params");

            return method(method,params);

        } catch (Exception e) {
            logger.error("[req error by find hospital]",e);
            msg = "获取失败";
            if(e instanceof NullQueryResultException){
                return Result.notResult("无查找结果",null);
            }
        }
        return Result.fail(msg);
    }

    @RequestMapping("/selectHospitalById")
    @ResponseBody
    public Result selectHospitalsById(@RequestBody JSONObject json
            , HttpServletRequest request){
        String msg = "请求失败 无授权信息";
        String token = request.getHeader(AUTH_TOKEN);
        if(!tokenMap.containsKey(token)){
            logger.error("[not token]");
            return Result.fail(msg);
        }
        try{
            Long id = json.getLong("hospitalId");
            return Result.success(hospitalService.getHospitalsById(id));
        }catch(Exception e){
            logger.error("[get hospital message failed]");
            msg = "获取信息失败";
        }

        return Result.fail(msg);
    }

    private Result method(String method, Map<String,Object> params)
            throws Exception{
        List<HospitalEntity> list;
        switch(method){
            case "all": case "all-page":{
                list = hospitalService.getAllHospitals();
            }
            break;
            default:
                logger.error("unknown method=>" + method);
                return Result.fail("未知查找方法");
        }
        if(list == null || list.isEmpty())
            throw new NullQueryResultException("[not result fo doctor]");

        //封装 带页数数据
        if(method.contains("page")){
            Integer current = Integer.valueOf(String.valueOf(params.get("current"))) ;
            Integer size = Integer.valueOf(String.valueOf(params.get("size")));
            Page<HospitalEntity> page = new Page<>(list, size, current);
            return Result.success(page.getPageDate());
        }

        return Result.success(list);
    }

}
